import java.util.Stack;

public class Solution {
    public String removeDuplicates(String s) {
        StringBuilder ret = new StringBuilder();
        Stack<Character> stack = new Stack<>();
        int n = s.length();
        int i = 0;
        while(i < n){
            //i指向的字符与栈顶元素是否相等
            if(!stack.isEmpty() && s.charAt(i) == stack.peek()){
                stack.pop();
            }
            else stack.push(s.charAt(i));
            i++;
        }
        for (Character ch : stack) {
            ret.append(ch);
        }

        return ret.toString();
    }
}
